// SDL_03 [SDL-OpenGL NeHe's Lesson 02].nova // Using namespace declarations. using Library.OpenGL; using Library.SDL; // The application class. class SdlNeHeLesson02App { // Application class's "main" function. public static void main( String[] args ) { // Output the keyboard contols. Stream.write( "Keyboard controls:\n" + " esc = exit\n" ); // The size of the OpenGL window. int WINDOW_WIDTH = 640, WINDOW_HEIGHT = 480; // Initialize SDL. if ( SDL.SDL_Init( SDL.SDL_INIT_VIDEO ) < 0 ) { // Report an error if it fails. Stream.writeLine( "Failed initializing SDL Video: " + SDL.SDL_GetError( ) ); // Return instead of SDL_Quit because SDL isn't yet initialized. return; } // Create an OpenGL screen. if ( SDL.SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL.SDL_OPENGL ) == null ) { Stream.writeLine( "Unable to create OpenGL screen: " + SDL.SDL_GetError( ) ); SDL.SDL_Quit( ); return; } // Set the title bar in environments that support it. String caption = "SDL_03 [SDL-OpenGL NeHe's Lesson 02]"; SDL.SDL_WM_SetCaption( caption, caption ); // Loop, drawing and checking events. initGL( WINDOW_WIDTH, WINDOW_HEIGHT ); bool done = false; SDL_Event event = new SDL_Event( ); while ( !done ) { drawGLScene( ); // Inner loop to process multiple SDL events per frame. while ( SDL.SDL_PollEvent( event ) == 1 ) { if ( event.typeID == SDL.SDL_QUIT ) { done = true; } if ( event.typeID == SDL.SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDL.SDLK_ESCAPE ) { done = true; } } } } // Shutdown SDL. SDL.SDL_Quit( ); } // A general OpenGL initialization function. // We call this right after our OpenGL window is created. private static void initGL( int width, int height ) { OpenGL.glViewport( 0, 0, width, height ); OpenGL.glClearDepth( 1.0 ); // Enables clearing of the depth buffer. OpenGL.glDepthFunc( OpenGL.GL_LESS ); // The type of depth test to do. OpenGL.glEnable( OpenGL.GL_DEPTH_TEST ); // Enables depth testing. OpenGL.glMatrixMode( OpenGL.GL_PROJECTION ); OpenGL.glLoadIdentity( ); // Reset the projection matrix. // Calculate the aspect ratio of the window. OpenGL.gluPerspective( 45.0, (double)width / (double)height, 0.1, 100.0 ); OpenGL.glMatrixMode( OpenGL.GL_MODELVIEW ); } // The main drawing function. private static void drawGLScene( ) { // Clear the screen and the depth buffer. OpenGL.glClear( OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT ); OpenGL.glLoadIdentity( ); // Reset the view. OpenGL.glTranslatef( -1.5f, 0.0f, -6.0f ); // Move left 1.5 units and into the screen 6.0 units. // Draw a triangle. OpenGL.glBegin( OpenGL.GL_POLYGON ); // Start drawing a polygon. OpenGL.glVertex3f( 0.0f, 1.0f, 0.0f ); // Top. OpenGL.glVertex3f( 1.0f, -1.0f, 0.0f ); // Bottom right. OpenGL.glVertex3f( -1.0f, -1.0f, 0.0f ); // Bottom left. OpenGL.glEnd( ); // We're done with the polygon. OpenGL.glTranslatef( 3.0f, 0.0f, 0.0f ); // Move right 3 units. // Draw a square (quadrilateral). OpenGL.glBegin( OpenGL.GL_QUADS ); // Start drawing a polygon (4 sided). OpenGL.glVertex3f( -1.0f, 1.0f, 0.0f ); // Top left. OpenGL.glVertex3f( 1.0f, 1.0f, 0.0f ); // Top right. OpenGL.glVertex3f( 1.0f, -1.0f, 0.0f ); // Bottom right. OpenGL.glVertex3f( -1.0f, -1.0f, 0.0f ); // Bottom left. OpenGL.glEnd( ); // Done with the polygon. // Swap buffers to display, since we're double buffered. SDL.SDL_GL_SwapBuffers( ); } }